package com.bosssoft.trainee.rbac.service.impl;

import com.bosssoft.trainee.rbac.dao.UserDao;
import com.bosssoft.trainee.rbac.entity.dto.UserDTO;
import com.bosssoft.trainee.rbac.exception.UsersException;
import com.bosssoft.trainee.rbac.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao usersMapper;

    /**
     * 用户登录
     */
    @Override

    public UserDTO userLogin(String username, String userPwd) {
        log.info("username:"+username);
        UserDTO userDTO = usersMapper.selectUserByName(username);
        log.info("查询结果："+userDTO);
        if(userDTO == null){
            throw new UsersException("用户不存在或密码有误");
        }else if(!userDTO.getUserPwd().equals(userPwd)){
            log.info("username:"+username);
            log.info("userPwd:"+userPwd);
            throw new UsersException("密码有误");
        }
        return userDTO;
    }
    /**
     * 注销用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userDelete(String username) {
        return usersMapper.deleteUser(username);
    }
    /**
     * 添加用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userInsert(UserDTO userDTO) {
        return  usersMapper.insertUser(userDTO);
    }
    /**
     * 修改用户密码
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userUpdatePwd(UserDTO userDTO) {
        return usersMapper.updateUserPwd(userDTO);
    }
    /**
     * 修改用户权限
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userUpdateRole(UserDTO userDTO) {
        return  usersMapper.updateUserRole(userDTO);
    }

    @Override
    public List<UserDTO> getAllUser() {
        return usersMapper.getAllUser();
    }

    @Override
    public UserDTO getUserById(Integer userId) {
        return usersMapper.getUserById(userId);
    }

    @Override
    public UserDTO getUserIsAdmin(Integer userId) {
        return usersMapper.getUserIsAdmin(userId);
    }

}
